﻿using System;
using System.Collections;
using System.Reflection;

using DevComponents.DotNetBar;

using System.Drawing;
using System.Windows.Forms;

using System.Runtime.InteropServices;
namespace Shine.Framework
{

    public class CommandPoolExClass : ICommandPoolEx
    {
        private Hashtable m_HashtableCommand = null;
        private Hashtable m_HashtableBaseItem = null;
        private object m_Hook;

        public CommandPoolExClass()
        {
            m_HashtableCommand = new Hashtable();
        m_HashtableBaseItem = new  Hashtable();
        }

        public BaseItem AddCommand(object obj)
        {
            if (FindCommand(obj.ToString()) == null)
                this.m_HashtableCommand.Add(obj.ToString(), obj);
            if (obj is UserControl)
            {
                (obj as ICommand).OnCreate(m_Hook);
                ControlContainerItem pItem = new ControlContainerItem();
                pItem.AllowItemResize = false;
                UserControl myControl = obj as UserControl;              
                 myControl.Dock = DockStyle.Fill;
                pItem.Control = myControl;           
                pItem.AllowItemResize = false;
                pItem.MenuVisibility = DevComponents.DotNetBar.eMenuVisibility.VisibleAlways;
                pItem.Name = obj.ToString();      
                //this.m_HashtableBaseItem.Add(pItem); 
               if(  m_HashtableBaseItem.ContainsKey( obj.ToString())==false )
                   this.m_HashtableBaseItem.Add(obj.ToString(),pItem); 
                return pItem;
            }
            //else if (obj is IToolControl)
            //{
            //   Control ctl=  Control.FromHandle(new IntPtr((obj as IToolControl).hWnd));
            //   ControlContainerItem pItem = new ControlContainerItem();
            //   pItem.AllowItemResize = false;           
            //   ctl.Dock = DockStyle.Fill;
            //   pItem.Control = ctl;           
            //   pItem.AllowItemResize = false;
            //   pItem.MenuVisibility = DevComponents.DotNetBar.eMenuVisibility.VisibleAlways;
            //   pItem.Name = obj.ToString();
            // //  this.m_HashtableBaseItem.Add(pItem);
            //   if (m_HashtableBaseItem.ContainsKey(obj.ToString()) == false)
            //       this.m_HashtableBaseItem.Add(obj.ToString(), pItem); 
            //   return pItem;

            //}
            else if (obj is ICommand)
            {
                ICommand pCommand = (ICommand)obj;
                ButtonItem m_ButtonItem = new ButtonItem();
                m_ButtonItem.Name = obj.ToString();
                m_ButtonItem.Text = pCommand.Caption;
                m_ButtonItem.ButtonStyle = eButtonStyle.ImageAndText;
                m_ButtonItem.ImagePosition = eImagePosition.Top;
                m_ButtonItem.ImageFixedSize = new Size(32, 32);
                if (pCommand.Bitmap != 0)
                {
                    Bitmap m_bitmap = System.Drawing.Bitmap.FromHbitmap((IntPtr)pCommand.Bitmap);
                    m_bitmap.MakeTransparent();
                    m_ButtonItem.Image = m_bitmap;
                }
                m_ButtonItem.Tooltip = pCommand.Tooltip;
                m_ButtonItem.Tag = obj;
               
                if (m_HashtableBaseItem.ContainsKey(obj.ToString()) == false)
                    this.m_HashtableBaseItem.Add(obj.ToString(), m_ButtonItem); 
                return m_ButtonItem;
            }
            return null;
        }
        public BaseItem FindBaseItem(string commandName)
        {
            if (m_HashtableBaseItem[commandName] != null)
                return m_HashtableBaseItem[commandName] as BaseItem;
            else
                return null;
        }
        public ICommand FindCommand(string commandName)
        {
            if (m_HashtableCommand[commandName] != null)
                return m_HashtableCommand[commandName] as ICommand;
            else
                return null;
        }

        public ICommand get_Command(int index)
        {
            if (index < this.m_HashtableCommand.Count && index > -1)
                return (m_HashtableCommand[index] as ButtonItem).Tag as ICommand;
            else
                return null;
        }

        public ICommand LoadCommand(string fileName, string className)
        {
            Assembly assembly = Assembly.Load(fileName);
            object provider = assembly.CreateInstance(className);
            if (provider is ICommand)
            {
                return provider as ICommand;
            }
            return null;
        }

        public ICommand LoadCommand(string fileName, string className, int subType)
        {
            //Assembly assembly = Assembly.Load(fileName);
            //object provider = assembly.CreateInstance(className);
            //if (provider is ICommandSubType)
            //{
            //    ICommandSubType pCommandSubType = provider as ICommandSubType;
            //    pCommandSubType.SetSubType(subType);
            //    return provider as ICommand;
            //}
            return null;
        }

        public void LoadCommands(string fileName)
        {
            Assembly assembly = Assembly.Load(fileName);

            //todo
        }

        public void UpdateCommandState()
        {
            try
            {
                BaseItem m_BaseItem = null;
                ICommand m_Command = null;

                IDictionaryEnumerator pEnumerator = m_HashtableBaseItem.GetEnumerator();
                pEnumerator.Reset();

                while (pEnumerator.MoveNext())
                {
                    m_BaseItem = pEnumerator.Value as BaseItem;
                    m_Command = this.FindCommand(m_BaseItem.Name);
                    m_Command.OnCreate(Hook);
                    try
                    {
                        if (m_BaseItem.Enabled != m_Command.Enabled)
                            m_BaseItem.Enabled = m_Command.Enabled;

                        if (m_BaseItem is ButtonItem )
                        {
                            if((m_BaseItem as ButtonItem).Checked != m_Command.Checked)
                                 (m_BaseItem as ButtonItem).Checked = m_Command.Checked;
                            if((m_Hook as IFramework).Tool==m_Command)
                                (m_BaseItem as ButtonItem).Checked = true;
                           
                        }
                        if (m_BaseItem is ControlContainerItem && (m_BaseItem as ControlContainerItem).Control.Enabled != m_Command.Enabled)
                        {
                            (m_BaseItem as ControlContainerItem).Control.Enabled = m_Command.Enabled;
                        }
                    }
                    catch (Exception ex)
                    {
                        //MessageBox.Show(m_Command.ToString() + "重写出现问题!" + "\r\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                //ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(m_BaseItem);
                //ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(m_Command);
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }

        public void Remove(ICommand pCommand)
        {
            this.m_HashtableCommand.Remove(pCommand.Name);
        }

        public void RemoveAll()
        {
            this.m_HashtableCommand.Clear();
        }

        public void SetHook(object pHook)
        {
            m_Hook = pHook;
        }

        public int Count
        {
            get
            {
                return m_HashtableCommand.Count;
            }
        }

        public object Hook
        {
            get
            {
                return m_Hook;
            }
            set
            {
                this.m_Hook = value;
            }
        }
    }
}

